package xh.ketama;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by xh on 2016/3/8.
 */
public class Main {

    public static void main(String[] args) {

        List<Node> nodes = new ArrayList<Node>();
        nodes.add(new Node("node1", 4));
        nodes.add(new Node("node2", 3));
        nodes.add(new Node("node3", 2));
        nodes.add(new Node("node4", 1));


        Multiset<Node> o = HashMultiset.create();

        KetamaRouter kr = new KetamaRouter(nodes, 400);

        int total = 100000;
        for (int i = 0; i < total; i++) {
            o.add(kr.hitNode(randomStr(11)));
        }

        System.out.println("Total hit times: " + total);
        for (Node n : nodes) {
            int count = o.count(n);
            System.out.println(n.name + " hit " + count + " times, percent: " + ((float )count * 100 / total) + "%");
        }
    }


    private static String DICT = "1234567890abcdefghijklmnopqrstuvwxyz";

    private static String randomStr(int length) {
        Random random = new Random(System.nanoTime());
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            sb.append(DICT.charAt(random.nextInt(DICT.length())));
        }
        return sb.toString();
    }
}
